!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine write_vtr_header(file_unit, var_name, nx, ny, nz, n_var, var_dim, format, data_offset)
!==================================================================================================================================|

   implicit none

   integer(4), parameter :: n_dim = 3
   integer(4), parameter :: n_point_cell = 8

   integer(4), intent(in) :: nx, ny, nz, n_var, format, file_unit
   integer(8), intent(inout) :: data_offset

   character(*), intent(in) :: var_name(n_var)

   integer(4), intent(in) :: var_dim(n_var)

   character(12) :: int_num1, int_num2

   integer(4) :: int_size = 4, float_size = 4
   integer(4) :: i_var, index_var
   integer(4) :: n_point, n_cell

!----------------------------------------------------------------------------------------------------------------------------------|
!  prepare
!----------------------------------------------------------------------------------------------------------------------------------|
   n_point = nx * ny * nz
   n_cell = (nx - 1) * (ny - 1) * (nz - 1)

!----------------------------------------------------------------------------------------------------------------------------------|
!  binary
!----------------------------------------------------------------------------------------------------------------------------------|
   if (format .eq. 0) then
!----------------------------------------------------------------------------------------------------------------------------------|
!     write the info data
!----------------------------------------------------------------------------------------------------------------------------------|
      write(int_num1, '(i12)') n_point
      write(int_num2, '(i12)') n_cell
      write(file_unit) '<Piece NumberOfPoints="' // int_num1 // '" NumberOfCells="' // int_num2 // '">' // char(10)

      write(file_unit) '<PointData>' // char(10)
      index_var = 1
      do i_var = 1, n_var
!----------------------------------------------------------------------------------------------------------------------------------|
!        write the scalar point data
!----------------------------------------------------------------------------------------------------------------------------------|
         if (var_dim(i_var) .eq. 1) then
            write(int_num1, '(i12)') data_offset
            write(file_unit) '<DataArray type="Float32" format="appended" NumberOfComponents="1" Name="' //                        &
               trim(var_name(i_var)) // '" offset="' // int_num1 // '"/>' // char(10)
            data_offset = data_offset + int_size + n_point * float_size
            index_var = index_var + var_dim(i_var)
!----------------------------------------------------------------------------------------------------------------------------------|
!        write the vector point data
!----------------------------------------------------------------------------------------------------------------------------------|
         elseif (var_dim(i_var) .eq. 3) then
            write(int_num1, '(i12)') data_offset
            write(file_unit) '<DataArray type="Float32" format="appended" NumberOfComponents="3" Name="' //                        &
               trim(var_name(i_var)) // '" offset="' // int_num1 // '"/>' // char(10)
            data_offset = data_offset + int_size + 3 * n_point * float_size
            index_var = index_var + var_dim(i_var)
         endif
      enddo
      write(file_unit) '</PointData>' // char(10)

      write(file_unit) '<Points>' // char(10)
      write(int_num1, '(i12)') data_offset
      write(file_unit) '<DataArray type="Float32" format="appended" Name="coordinates" NumberOfComponents="3" offset="' //         &
         int_num1 // '"/>' // char(10)
      data_offset = data_offset + int_size + 3 * n_point * float_size
      write(file_unit) '</Points>' // char(10)

      write(file_unit) '<Cells>' // char(10)
      write(int_num1, '(i12)') data_offset
      write(file_unit) '<DataArray type="Int32" format="appended" Name="connectivity" offset="' //                                 &
         int_num1 // '"/>' // char(10)
      data_offset = data_offset + int_size + n_point_cell * n_cell * int_size
      write(int_num1, '(i12)') data_offset
      write(file_unit) '<DataArray type="Int32" format="appended" Name="offsets" offset="' //                                      &
         int_num1 // '"/>' // char(10)
      data_offset = data_offset + int_size + n_cell * int_size
      write(int_num1, '(i12)') data_offset
      write(file_unit) '<DataArray type="Int32" format="appended" Name="types" offset="' //                                        &
         int_num1 // '"/>' // char(10)
      data_offset = data_offset + int_size + n_cell * int_size
      write(file_unit) '</Cells>' // char(10)
      write(file_unit) '</Piece>' // char(10)
   endif

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine write_vtr_header
